//
//  RBTextField.m
//  RBDemoProj
//
//  Created by Lan on 2019/1/3.
//  Copyright © 2019年 SummerTea. All rights reserved.
//

#import "RBTextField.h"
#import "NSString+RBAdd.h"
#import <NSString+YYAdd.h>
#import "RanTool.h"

@interface RBTextField()

@end

@implementation RBTextField

#pragma mark - System

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setup];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self setup];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.text = self.text;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark -

- (void)setText:(NSString *)text
{
    [super setText:text];
    if (self.textChangeBlock)
    {
        self.textChangeBlock(self.text);
    }
}

- (void)setType:(RBTextFieldType)type
{
    _type = type;
    switch (type)
    {
        case RBTextFieldTypePhone:
        {
            self.keyboardType = UIKeyboardTypeNumberPad;
            self.maxTextLength = 11;
        }
            break;
        case RBTextFieldTypePassword:
        {
            self.keyboardType = UIKeyboardTypeEmailAddress;
            self.secureTextEntry = NO;
        }
            break;
        case RBTextFieldTypePasswordSecurity:
        {
            self.keyboardType = UIKeyboardTypeEmailAddress;
            self.secureTextEntry = YES;
        }
            break;
        case RBTextFieldTypeIdCard:
        {
            self.keyboardType = UIKeyboardTypeEmailAddress;
            self.maxTextLength = 18;
        }
            break;
        case RBTextFieldTypeEmail:
        {
            self.keyboardType = UIKeyboardTypeEmailAddress;
        }
            break;
        case RBTextFieldTypeChinese:
        {
            self.keyboardType = UIKeyboardTypeDefault;
        }
            break;
        case RBTextFieldTypeNumber:
        {
            self.keyboardType = UIKeyboardTypeNumberPad;
        }
            break;
        case RBTextFieldTypeChar:
        {
            self.keyboardType = UIKeyboardTypeEmailAddress;
        }
            break;
        case RBTextFieldTypeNumberChar:
        {
            self.keyboardType = UIKeyboardTypeEmailAddress;
        }
            break;
        case RBTextFieldTypePrice:
        {
            self.keyboardType = UIKeyboardTypeDecimalPad;
            self.text = @"0";
        }
            break;
        case RBTextFieldTypeInt:
        {
            self.keyboardType = UIKeyboardTypeNumberPad;
        }
            break;
        default:
            break;
    }
}

- (BOOL)validate
{
    NSString *text = self.trimOnValidation? self.text.stringByTrim: self.text;
    if (self.validateExpression)
    {
        return [text rbValidateWithExpression:self.validateExpression];
    }
    switch (self.type)
    {
        case RBTextFieldTypePhone:
        {
            return [text rbPhone];
        }
            break;
        case RBTextFieldTypePassword:
        {
            return [text rbPassword];
        }
            break;
        case RBTextFieldTypePasswordSecurity:
        {
            return [text rbPassword];
        }
            break;
        case RBTextFieldTypeIdCard:
        {
            return [text rbIdCardNum];
        }
            break;
        case RBTextFieldTypeEmail:
        {
            return [text rbEmail];
        }
            break;
        case RBTextFieldTypeChinese:
        {
            return [text rbChinese];
        }
            break;
        case RBTextFieldTypeNumber:
        {
            return [text rbNumber];
        }
            break;
        case RBTextFieldTypeChar:
        {
            return [text rbChar];
        }
            break;
        case RBTextFieldTypeNumberChar:
        {
            return [text rbCharNum];
        }
            break;
        case RBTextFieldTypePrice:
        {
            return YES;
        }
            break;
        default:
            break;
    }
    return YES;
}

#pragma mark - Private

- (void)setup
{
    self.minInt = 1;
    self.maxInt = 999999;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextFieldTextDidChangeNotification object:nil];
    self.trimOnValidation = YES;
}

#pragma mark - Event

- (void)textChanged: (NSNotification *)noti
{
    if (noti.object == self)
    {
        if (self.type == RBTextFieldTypePrice)
        {
            NSString *text = self.text;
            if ([text containsString:@"."])
            {
                if ([[text substringFromIndex:text.length - 1] isEqualToString:@"."] && [text rangeOfString:@"."].location != text.length - 1)
                {//去多余的.
                    text = [text substringToIndex:text.length - 1];
                }
                else if(text.length == 1)
                {//加个0
                    text = @"0.";
                }
            }
            else
            {
                //去前面的0
                while (text.length > 1 && [text hasPrefix:@"0"])
                {
                    text = [text substringFromIndex:1];
                }
            }
            self.text = text;
        }
        else if (self.type == RBTextFieldTypeInt)
        {
            NSString *text = self.text;
            if (text.length)
            {
                NSInteger intNum = text.integerValue;
                if (intNum < self.minInt)
                {
                    intNum = self.minInt;
                }
                if(intNum > self.maxInt)
                {
                    intNum = self.maxInt;
                }
                self.text = [NSString stringWithFormat:@"%zd", intNum];

            }
        }
        else
        {
            if ((!self.markedTextRange || self.markedTextRange.isEmpty))
            {
                if (self.maxTextLength > 0 && self.text.length > self.maxTextLength)
                {
                    self.text = [self.text substringToIndex:self.maxTextLength];
                }
                else
                {
                    if (self.textChangeBlock)
                    {
                        self.textChangeBlock(self.text);
                    }
                }
            }
        }
    }
}

@end
